package com.enbatis.mybatisplugs.service.impl;

import com.enbatis.mybatisplugs.base.Account;
import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.base.IRedisService;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.entity.SysResource;
import com.enbatis.mybatisplugs.entity.SysUser;
import com.enbatis.mybatisplugs.entity.dto.Menu;
import com.enbatis.mybatisplugs.entity.dto.MenuDTO;
import com.enbatis.mybatisplugs.entity.vo.SysResourceVO;
import com.enbatis.mybatisplugs.mapper.SysResourceMapper;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.enbatis.mybatisplugs.service.SysResourceService;
import com.enbatis.mybatisplugs.service.SysUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* 描述：
* @author wangweidong
* @date 2019-06-18 12:41:26
*/

@Service
public class SysResourceServiceImpl extends BaseServiceImpl<SysResourceMapper,SysResource > implements SysResourceService {


    @Autowired
    private IRedisService redisService;
    @Autowired
    private SysUserService userService;

    @Override
    public SysResource addResource(SysResource resource) {
        resource.setCtime(new Date());
        SysResource sysResource= insert(resource);
        return sysResource;
    }


    @Override
    public List<SysResource> getUserSysResource(SysUser sysUser){

      return  baseMapper.selectHaveAuthListList(sysUser.getId());
    }


    @Override
    public List<MenuDTO> getMenuList(SysResource sysResource, Account sysUser) {


        List<MenuDTO>  menuDTOS=(List<MenuDTO>) redisService.getList("getMenuList"+sysUser.getId());
        if (CollectionUtils.isNotEmpty(menuDTOS)){
            return menuDTOS;
        }

            List<MenuDTO> dtoList=new ArrayList<>();
            List<SysResource> allList= this.list(new Wrapper<>(sysResource).setSqlSelect("id","pid","name","url","code","pic","type").orderBy("order_by",false));
            Map<Long,SysResource> allMap=allList.stream().collect(Collectors.toMap(SysResource::getId, s -> s));
            List<SysResource>  haveAuthList=   baseMapper.selectHaveAuthListList(sysUser.getId());
            Map<Long,SysResource> parentListMap=new LinkedHashMap<>();
            for (SysResource resource:haveAuthList) {
                if (resource.getPid()==0){
                    parentListMap.put(resource.getId(),resource);
                }else{
                    SysResource sysR=allMap.get(resource.getPid());
                    while (true){
                        if (sysR.getPid().equals(0L)){
                            if (!parentListMap.containsValue(sysR.getId())){
                                parentListMap.put(sysR.getId(),sysR);
                                break;
                            }
                        }else{
                            sysR=allMap.get(sysR.getPid());
                        }
                    }
                }
            }
            for (Map.Entry<Long,SysResource> resource:parentListMap.entrySet()) {
                MenuDTO dto=new MenuDTO();
                dto.setMenuName(resource.getValue().getName());
                dto.setPath(resource.getValue().getUrl());
                dto.setName(resource.getValue().getCode());
                dto.setPid(resource.getValue().getPid());
                dto.setIcon(resource.getValue().getPic());
                if (null!=resource.getValue().getType()){
                    dto.setType(resource.getValue().getType()+"");
                }

                List<SysResource> childList=new ArrayList<>();
                for (SysResource res:haveAuthList) {
                    if (res.getPid().equals(resource.getValue().getId())){
                        childList.add(res);
                    }
                }
                if (!CollectionUtils.isEmpty(childList)){
                    List<Menu> menusList=new ArrayList<>();
                    for (SysResource r:childList) {
                        Menu menu=new Menu();
                        menu.setMenuName(r.getName());
                        menu.setPath(r.getUrl());
                        menu.setName(r.getCode());
                        menu.setPid(r.getPid());
                        menu.setIcon(r.getPic());
                        menusList.add(menu);
                    }
                    dto.setChildren(menusList);
                }
                dtoList.add(dto);
            }
            redisService.setList("getMenuList"+sysUser.getId(),dtoList);
            return dtoList;
    }


    @Override
    public int updateResourceById(SysResource sysResource,Account sysUser) {
        List<SysUser> sysUserList = userService.list(new Wrapper<>(new SysUser()).setSqlSelect("id"));
        for (SysUser user:sysUserList) {
            redisService.remove("getMenuList"+user.getId());
        }

        return updateById(sysResource);
    }


    @Override
    public List<MenuDTO> getMenuVOList(SysResourceVO vo) {
        List<SysResourceVO> resourceVOList=baseMapper.selectVOByRoleList(vo);
        List<MenuDTO> dtoList=new ArrayList<>();
        for (SysResourceVO resourceVO:resourceVOList) {
            if (resourceVO.getPid()==0){
                MenuDTO dto=new MenuDTO();
                dto.setId(resourceVO.getId());
                dto.setLabel(resourceVO.getName());
                dto.setChecked(resourceVO.getChecked());
                dto.setPid(resourceVO.getPid());
                dtoList.add(dto);
            }
        }
        for (MenuDTO menuDTO:dtoList) {
            List<Menu> menusList=new ArrayList<>();
            for (SysResourceVO resourceVO:resourceVOList){
                if (menuDTO.getId().equals(resourceVO.getPid())){
                      Menu menu=new Menu();
                      menu.setId(resourceVO.getId());
                      menu.setLabel(resourceVO.getName());
                      menu.setChecked(resourceVO.getChecked());
                      menu.setPid(resourceVO.getPid());
                      menusList.add(menu);
                }
            }
            menuDTO.setChildren(menusList);
        }
        return dtoList;
    }


    @Override
    public Pages<SysResourceVO> pageVO(Page<SysResourceVO> page, SysResourceVO vo) {
        Page returnPage= PageHelper.startPage(page.getPageNum(),page.getPageSize(),page.getOrderBy());
        baseMapper.selectVOList(vo);
        return new Pages<>(returnPage);
    }



}
